﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using Newtonsoft.Json;
using CommonUtility.Utility;
using DataAccess;
using System.Data;

namespace UIDevCore
{
    public class UIHelper
    {
        public static PostData GetPostData(HttpContext context)
        {
            PostData postData = null;
            using (StreamReader sr = new StreamReader(context.Request.InputStream))
            {
                postData = JsonConvert.DeserializeObject<PostData>(sr.ReadToEnd());
            }
            return postData;
        }

        private static string GetClient()
        {
            UIPremission pre = new UIPremission();
            if (pre.IsLogin)
                return JsonHelper.SerializeData(pre.CurrentUser);
            return null;
        }

        public static string GetErrorResponseData(UIEmnus.EError errorType)
        {
            return JsonHelper.SerializeData(new ResponseData() { ErrorType = (short)errorType});
        }

        public static string GetResponseData(object data,string error)
        {
            ResponseData responseData = new ResponseData();
            if (data != null)
                responseData.Body = JsonHelper.SerializeData(data);
            if (error != null)
            {
                responseData.ErrorType = (short)UIEmnus.EError.Normal;
                responseData.Error = error;
            }

            return JsonHelper.SerializeData(responseData);
        }

        public static PostListSetting GetListSetting(string jsonData)
        {
            return JsonHelper.DeserializeData<PostListSetting>(jsonData);
        }

        #region 获取json字典的值
        public static object getDicValue(string paramName, Dictionary<String, Object> divValues)
        {
            if (divValues.ContainsKey(paramName))
                return divValues[paramName];
            return null;
        }

        public static T getDicValue<T>(string paramName, Dictionary<String, Object> divValues, T defaultValue)
        {
            object value = getDicValue(paramName, divValues);
            if (value != null)
                return CommonUtility.Utility.CommonHelper.Convert_String<T>(value.ToString(), defaultValue);

            return defaultValue;
        }
        #endregion
    }
}
